package com.dongdongshop.itempage.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbItemCat;
import com.dongdongshop.operator.service.ItemCatService;
import com.dongdongshop.seller.pojo.TbGoods;
import com.dongdongshop.seller.pojo.TbGoodsDesc;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.GoodsDescService;
import com.dongdongshop.seller.service.GoodsService;
import com.dongdongshop.seller.service.ItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
@RocketMQMessageListener(topic = "goods_goodsId",consumerGroup = "dongdongconsumer")
public class ItemPageServiceImpl implements RocketMQListener<Long> {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${freemarker.pageDir}")
    private String pageDir;

    @Reference(check = false,timeout = 500000)
    private GoodsService goodsService;
    @Reference(check = false,timeout = 500000)
    private GoodsDescService goodsDescService;
    @Reference(check = false,timeout = 500000)
    private ItemCatService itemCatService;
    @Reference(check = false,timeout = 500000)
    private ItemService itemService;

    @Override
    public void onMessage(Long goodsId) {
        //加载配置
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        //创建模板
        try {
            Template template = configuration.getTemplate("item.ftl");
            Map map = new HashMap();

            //查询goods表
            TbGoods goods = goodsService.getGoodsByGoodsId(goodsId);
            //查询goodsDesc表
            TbGoodsDesc goodsDesc = goodsDescService.getgoodsDescByGoodsId(goodsId);

            //查询一级、二级、三级类型
            TbItemCat itemCat1=itemCatService.selectbyid(goods.getCategory1Id());
            TbItemCat itemCat2=itemCatService.selectbyid(goods.getCategory2Id());
            TbItemCat itemCat3=itemCatService.selectbyid(goods.getCategory3Id());

            //查询商品类型

            List<TbItem> items=itemService.selectbygoodsid(goodsId);


            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);
            map.put("items",items);
            //创建输出流
            fileWriter = new FileWriter(pageDir+goodsId+".html");
            //使用模板输出
            template.process(map,fileWriter);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileWriter != null){
                try {
                    fileWriter.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
